package com.xiangyi.teachingsystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiangyi.teachingsystem.entity.po.TaskSum;
import com.xiangyi.teachingsystem.entity.vo.Result;
import com.xiangyi.teachingsystem.service.TaskSumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.sql.ResultSet;
import java.util.List;

@RestController
@RequestMapping("/taskSum")
public class TaskSumController {
    @Autowired
    TaskSumService taskSumService;

    //@PostMapping("/addTaskSum")
    //public Result add(@RequestBody TaskSum taskSum){
        //Integer taskId = taskSum.getTaskId();
        //Integer groupId = taskSum.getGroupId();
        //TaskSum checkSum = taskSumService.getOne(new QueryWrapper<TaskSum>()
        //        .eq("task_id", taskId)
        //        .eq("group_id", groupId));
        //if (checkSum!=null){
        //    return Result.failure("报告已存在，请勿重复提交");
        //}
    //    try {
    //        taskSumService.save(taskSum);
    //        return Result.success("任务总结保存成功");
    //    }catch (Exception e){
    //        e.printStackTrace();
    //        return Result.failure("服务器错误").setData(e.getMessage());
    //    }
    //}

    @PostMapping("/addOrUpdateTaskSum")
    public Result updateTaskSum(@Validated @RequestBody TaskSum taskSum){
        Integer taskId = taskSum.getTaskId();
        Integer groupId = taskSum.getGroupId();
        TaskSum checkSum = taskSumService.getOne(new QueryWrapper<TaskSum>()
                .eq("task_id", taskId)
                .eq("group_id", groupId));
        if (checkSum==null){
            try {
                taskSumService.save(taskSum);
                TaskSum sumInfo = taskSumService.getOne(new QueryWrapper<TaskSum>()
                        .eq("task_id", taskId)
                        .eq("group_id", groupId));
                return Result.success("任务总结保存成功").setData(sumInfo);
            }catch (Exception e){
                e.printStackTrace();
                return Result.failure("服务器错误").setData(e.getMessage());
            }
        }

        try {
            taskSumService.update(taskSum,new QueryWrapper<TaskSum>()
                    .eq("task_id",taskId)
                    .eq("group_id",groupId));
            TaskSum sumInfo = taskSumService.getOne(new QueryWrapper<TaskSum>()
                    .eq("task_id", taskId)
                    .eq("group_id", groupId));
            return Result.success("任务总结更新成功").setData(sumInfo);
        }catch (Exception e){
            e.printStackTrace();
            return Result.failure("服务器错误").setData(e.getMessage());
        }
    }
    @GetMapping("/saveSumById")
    public Result saveSumById(@RequestParam Integer sumId){
        try {
            TaskSum taskSum = new TaskSum();
            taskSum.setId(sumId);
            taskSum.setCommitStatus(true);
            taskSumService.updateById(taskSum);
            return Result.success("报告提交成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.failure("服务器错误").setData(e.getMessage());
        }

    }

    @GetMapping("/summaryList")
    public Result summaryList(@RequestParam Integer taskId){
        try {
            List<TaskSum> taskSums = taskSumService.getBaseMapper().selectList(new QueryWrapper<TaskSum>()
                    .eq("task_id", taskId));
            return Result.success("任务总结查询成功").setData(taskSums);
        }catch (Exception e){
            e.printStackTrace();
            return Result.failure("服务器错误").setData(e.getMessage());
        }
    }

    @GetMapping("/getSummaryByGroupId")
    public Result getSummaryByGroupId(@RequestParam Integer groupId){
        try {
            TaskSum sumByGroupId = taskSumService.getOne(new QueryWrapper<TaskSum>().eq("group_id", groupId));
            return Result.success("小组报告查询成功").setData(sumByGroupId);
        }catch (Exception e){
            e.printStackTrace();
            return Result.failure("服务器错误");
        }

    }
}
